import axios from 'axios'
import router from '@/plugins/router'
import { message } from 'ant-design-vue'
import { errorTips, responseCodeError } from './errorTips'
import { APIURL } from '@g/data'
function warnFunc(code: number) {
  let warnStr
  switch (code) {
    case 4000:
      warnStr = '参数错误'
      break;
    case 4001:
      warnStr = '拒绝访问'
      break;
    case 4003:
      warnStr = '拒绝访问'
      break;
    case 4010:
      warnStr = '验证码错误'
      break;
    case 4011:
      warnStr = '用户未登陆'
      break;
    case 4012:
      warnStr = '登陆超时'
      break;
    case 4013:
      warnStr = '用户被踢下'
      break;
    case 4014:
      warnStr = '用户未绑定手机号'
      break;
    case 4015:
      warnStr = '授权到期'
      break;
    case 5000:
      warnStr = '服务器异常'
      break;
    case 5101:
      warnStr = '用户不存在'
      break;
    case 5102:
      warnStr = '密码错误'
      break;
    case 5103:
      warnStr = '二维码过期'
      break;
    case 5105:
      warnStr = '已关注'
      break;
    default:
      break;
  }
  message.error(warnStr)
}
const axiosInstance = axios.create({
  baseURL: APIURL,
  timeout: 100000
})
// 没有任何配置的axios实例
const axiosNoConfig = axios.create({
  baseURL: APIURL,
  timeout: 100000
})
axiosInstance.interceptors.request.use(
  config => {
    if (!config.data) {
      config.data = {}
    }
    const token = localStorage.getItem('token') || ''
    config.headers = {
      'Content-Type': 'application/json',
      // token: "4d0bfe0c9c594f6bb20e60ac585646903059bac6"
      token: token
    }
    return config
  },
  error => Promise.reject(error)
)

let requestLock = false
// 添加响应拦截器
axiosInstance.interceptors.response.use(
  res => {
    requestLock = false // 关闭请求锁
    const {
      config: { url = '' }
    } = res
    const api = url.replace(APIURL, '') // 拿到后端的路由
    if (res.data.code === 0) {
      return res.data.content || {}
    } else {
      console.log(res.data);
      if (res.data.code !== 5104) {
        warnFunc(res.data.code)
      }
      return Promise.reject(res.data.code)
    }
  },
  // eslint-disable-next-line complexity
  error => {
    const { response: { status: resStatus = -1 } = {} } = error
    // const { response: { data: { failureReason: failReason } = { failureReason: 0 } } = {} } = error
    // const { response: { data: { code,message } = { code: 0 ,message:''} } = {} } = error
    const { response: { data } = { data: {} } } = error
    console.log(data);
    // const { response: { data: { failureReason: failReason } = { failureReason: 0 } } = {} } = error
    // const messageInfo = responseCodeError[failReason]
    const {
      config: { url, baseURL }
    } = error
    const api = url.replace(baseURL, '') // 拿到后端的路由
    warnFunc(data.code)
    if (resStatus === 4013 || data.code === 4011 || data.code === 4012) {
      if (!requestLock) {
        console.log('登录超时,请重新登录')
        localStorage.removeItem('token')
        // router.replace('/login')
      }
      requestLock = true
    }
    // eslint-disable-next-line
    return Promise.reject(data.code);
  }
)
export { axiosNoConfig }
export default axiosInstance
